package com.example.demo.entity;
import java.io.Serializable;

/**
 * Created by leeweit on 2018/3/1.
 */
public class Result<T> implements Serializable {

    private String code;
    private String msg;
    private T body;

    public Result(){
        this.code = String.valueOf(0);
        this.msg = "成功";
    }

    public Result(Builder<T> builder){
        this.code = String.valueOf(builder.code);
        this.msg = builder.msg;
        this.body = builder.body;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getBody() {
        return body;
    }

    public void setBody(T body) {
        this.body = body;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", body=" + body +
                '}';
    }

    public static class Builder<T>{
        private Integer code;
        private String msg;
        private T body;

        public Builder(Integer code){
            this.code = code;
        }

        public Builder(){

        }

        public Builder<T> code(Integer code){
            this.code = code;
            return this;
        }

        public Builder<T> msg(String msg){
            this.msg = msg;
            return this;
        }

        public Builder<T> body(T body){
            this.body = body;
            return this;
        }

        public Result<T> build(){
            return new Result<>(this);
        }

        public static <T> Builder<T> custom(T t){
            return new Builder<T>().body(t);
        }
    }

    public static Result success(){
        return success(null);
    }

    public static <T> Result<T> success(T t){
        return Builder.custom(t).code(0).msg("成功").build();
    }
}
